package com.zkgengkun.web.controller;

import com.google.common.collect.Maps;
import com.zkgengkun.console.service.BlogService;
import com.zkgengkun.constants.SysConstants;
import com.zkgengkun.handler.MyException;
import com.zkgengkun.support.component.AmqpSend;
import com.zkgengkun.support.redis.RedisUtils;
import com.zkgengkun.web.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

/**
 * @author gengshikun
 * @date 2016/12/6
 */
@Controller
public class WebIndexController {

    @Autowired
    private AmqpSend amqpSend;

    @Autowired
    private UserService userService;

    @Autowired
    private BlogService blogService;

    @RequestMapping(value = {"", "/index"}, method = RequestMethod.GET)
    public String index(Model model) {
        model.addAttribute("data", "Hello Springboot!");
        amqpSend.sendMsg("Hello Springboot!");
        Map<String, Object> map = Maps.newHashMap();
        map.put("hello", "Hello Map!");
        amqpSend.sendMap(SysConstants.AMQP_ROUTINGKEY_MAP, map);
        return "index";
    }

    @RequestMapping(value = "json", method = RequestMethod.GET)
    public String json(Model model) throws Exception {
        throw new MyException("sdfasdf");
    }

    @RequestMapping(value = "redis", method = RequestMethod.GET)
    @ResponseBody
    public String redis(){

//        RedisUtils.set("boot-test", "redis-test", 100);

        String test = (String) RedisUtils.get("boot-test");
        System.out.println(test);

        return "success";
    }

    @RequestMapping(value = "redis_service")
    @ResponseBody
    public Map redisService(Model model){
        Map<String, Object> result = Maps.newHashMap();
        result.put("blog1", this.blogService.getOne("4028b8815925228601592523054a0005").getBlogId());
        return result;
    }

}
